<?php
	date_default_timezone_set('America/New_York');
	require '../../slimloader/Slim/Slim.php';
	require '../helpers/GeneralUtil.php';
	require '../helpers/Config.php';
	require '../helpers/DbHelper.php';
	require '../flickr/phpFlickr.php';
	require '../helpers/Upload.php';
	\Slim\Slim::registerAutoloader();
	
	$app = new \Slim\Slim(array(
		'debug'=>true
	));
	$app->post('/data',function() use ($config,$app){
		$respData=array();
		$imageUrl=null;
		$thumbnail=null;
		$apikey = $app->request->post("devapipassword");
		if($apikey!=$config['devapipassword']){
			echo json_encode(generateError('0','Miss Match API Key'));
			exit;
		}
		$path = "tmp/";
		$dbHelper =new DbHelper();
		$con = mysqli_connect($config['database']['host'],$config['database']['username'],$config['database']['password'],$config['database']['table']);
		if (mysqli_connect_errno()){
				$respData=generateError('0',"Failed to connect to MySQL: " . mysqli_connect_error());
		}else{
			$handler = new Upload(@$_FILES['file']);
			$isImage = "1";
			$desc="";
			$uniFileName = uniqid("", true);
			$fileDist="";
			$handler->file_new_name_body=$uniFileName;
			if($handler->uploaded){
				$handler->Process($path);
				if($handler->processed){
					$fileDist=$handler->file_dst_pathname;
					$handler->clean();
					$flickr = new phpFlickr($config['flickr']['apiKey'],$config['flickr']['secretKey'],true);
					$flickr->setToken($config['flickr']['token']);
					$photoId = $flickr->sync_upload($fileDist);
					$photoSize = $flickr->photos_getSizes($photoId);
					$imageUrl = $photoSize[5]['source'];
					$thumbnail = $photoSize[0]['source'];
					$hasUnlinked=unlink($fileDist);
				}
			}else{
				$isImage="0";
				$desc=$app->request->post("description");
			}
			$zipcode = $app->request->post("zipcode");
                        if(preg_match('/^\d{5}$/',$zipcode)){
                            $phoneNumber = $app->request->post("phoneNumber");
                            if(preg_match('/^\d{10}$/',$phoneNumber)){
                                $jobType = $app ->request->post("jobType");
                                $jobCategory = $app->request->post("jobCategory");
                                $language = $app->request->post("language");
                                $success = $dbHelper->saveJob($isImage,$zipcode,$phoneNumber,$jobType,$language,$desc,"1",$imageUrl,$thumbnail,$jobCategory,$con);
                                if($success){
                                        $respData=generateError("1","Your Job has been saved!");
                                }else{
                                        $respData=generateError("0","There is an issue saving this job - ".mysqli_error($con));
                                }
                            }else{
                                $respData=  generateError("0", "Please enter a 10 digit phone number");
                            }
                        }else{
                            $respData = generateError("0","Please enter a 5 digit valid zipcode");
                        }
		}
		mysqli_close($con);
		echo json_encode($respData);
	});
	$app->run();
	
?>